package com.kx.yooli.kx.wechat.service.bill;

import com.google.common.collect.Lists;
import com.kx.yooli.kx.wechat.entity.nplm.CustInfo;
import com.kx.yooli.kx.wechat.entity.nplm.LoanRepayPlan;
import com.kx.yooli.kx.wechat.entity.nplm.OverdueCase;
import com.kx.yooli.kx.wechat.entity.repayment.ActiveRepayOperate;
import com.kx.yooli.kx.wechat.mapper.bill.BillMapper;
import com.kx.yooli.kx.wechat.mapper.nplm.*;
import com.kx.yooli.kx.wechat.mapper.user.UserInfoMapper;
import com.kx.yooli.kx.wechat.mapper.user.UserInfoSsoMapper;
import com.kx.yooli.kx.wechat.po.user.UserInfo;
import com.kx.yooli.kx.wechat.po.user.UserInfoSso;
import com.kx.yooli.kx.wechat.service.IBillService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author john_sqwen-childe.wen
 * date 2024/5/9
 * PROJECT_NAME wechat
 * description create BillServiceImpl by john_sqwen
 * and  value is
 */
@Service
public class BillServiceImpl implements IBillService {
    private final Logger log = LoggerFactory.getLogger(BillServiceImpl.class);
    @Value("${url.mgr.service}")
    private String url;
    public BillServiceImpl(UserInfoMapper userInfoMapper, ILoanPlanMapper iLoanPlanMapper, BillMapper billMapper) {
        this.userInfoMapper = userInfoMapper;
        this.iLoanPlanMapper = iLoanPlanMapper;
        this.billMapper = billMapper;
    }
    private final UserInfoMapper userInfoMapper;
    private final ILoanPlanMapper iLoanPlanMapper;
    private final BillMapper billMapper;

    @Resource
    private UserInfoSsoMapper userInfoSsoMapper;
    @Resource
    private NplmLoanContractMapper nplmLoanContractMapper;

    @Resource
    private OverdueCaseMapper overdueCaseMapper;

    @Resource
    private CustInfoMapper cus;

    @Override
    public List<LoanRepayPlan> selectCurrBill(String loanNo) {
        if(loanNo.isBlank()){
            return null;
        }
        List<LoanRepayPlan> loanRepayPlans =  Lists.newArrayList();
        loanRepayPlans.addAll( iLoanPlanMapper.selectLoanByLoanNo(loanNo));
        return  loanRepayPlans;
    }

    @Override
    public List<OverdueCase> selectCurrBillActive(String mobile) {
        if(mobile.isBlank()){
            return null;
        }
        List<UserInfoSso> userInfoSsos = null;
       // tempSet.addAll(nplmLoanContractMapper.selectLoanNoByCertNo(custInfo.getCustNo()));
       // tempSet.addAll(billMapper.selectLoanNoByCertNo(userInfo.getCertNo()));
        /**
         * desc new logic by nplm datasource
         *
         */
       // mobile = "17723307242";

        List<Map> strings = nplmLoanContractMapper.selectLoanNobyMobile(mobile);
        if(strings.isEmpty()){
           userInfoSsos = userInfoSsoMapper.selectIsExistCertNo(mobile);
        }
        String certNo = "";
        Set<String> tempSet = null;
        if(!strings.isEmpty()){
            List<String> tempLoanNo = org.apache.commons.compress.utils.Lists.newArrayList();
            for (Map string : strings) {
                tempLoanNo.add(string.get("LOAN_NO").toString());
            }
            tempSet = new HashSet<>(tempLoanNo);
        }else {
            certNo = userInfoSsos.getFirst().getCertNo();
            if (null != certNo && !certNo.isBlank()) {
                List<String> loanNo = nplmLoanContractMapper.selectLoanNoByIdCard(certNo);
                List<String> loanTemp = Lists.newArrayList();
                loanTemp.addAll(loanNo);
                tempSet = new HashSet<>(loanTemp);
            }
        }
        assert tempSet != null;
        List<String> billEntity = new ArrayList<>(tempSet);
        if (billEntity.isEmpty()){
            return null;
        }
        List<OverdueCase> loanRepayPlans =  Lists.newArrayList();
        for (String entity : billEntity) {
            if (!entity.isBlank()) {
                log.info("get loanNo value is  {}", entity);
                List<OverdueCase> overdueCases = overdueCaseMapper.selectOverDuCaseByLoanNoPro(entity);
                if (overdueCases.isEmpty()){
                    continue;
                }
                if(overdueCases.getFirst().getPlannedTotal().compareTo(overdueCases.getFirst().getOnceSettleAmount()) < 0){
                    overdueCases.getFirst().setPlannedTotal(overdueCases.getFirst().getOnceSettleAmount());
                }
                BigDecimal offline = iLoanPlanMapper.selectOverDuCaseByLoanNoOffline(entity);
                if (offline!=null && offline.compareTo(BigDecimal.ZERO) > 0){
                    BigDecimal newPosition =  overdueCases.getFirst().getPlannedTotal().subtract(offline);
                    overdueCases.getFirst().setPlannedTotal(newPosition);
                }
                loanRepayPlans.addAll(overdueCases);
            }else {
                break;
            }
        }
        return  loanRepayPlans;
    }

}
